package com.huangyi;

public class Main {
    public static void main(String[] args) {
        // 测试用例 - 两数相加
        Solution solution1 = new Solution();
        System.out.println("两数相加:");
        // l1 = [2,4,3], l2 = [5,6,4] -> 342 + 465 = 807 -> [7,0,8]
        ListNode l1 = new ListNode(2, new ListNode(4, new ListNode(3)));
        ListNode l2 = new ListNode(5, new ListNode(6, new ListNode(4)));
        ListNode result1 = solution1.addTwoNumbers(l1, l2);
        System.out.print("l1=[2,4,3], l2=[5,6,4]: ");
        printList(result1); // [7,0,8]

        // 测试用例 - 两两交换链表中的节点
        Solution2 solution2 = new Solution2();
        System.out.println("\n两两交换链表中的节点:");
        // [1,2,3,4] -> [2,1,4,3]
        ListNode head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4))));
        ListNode result2 = solution2.swapPairs(head);
        System.out.print("head=[1,2,3,4]: ");
        printList(result2); // [2,1,4,3]
    }

    // 辅助方法：打印链表
    static void printList(ListNode head) {
        System.out.print("[");
        while (head != null) {
            System.out.print(head.val);
            if (head.next != null) System.out.print(",");
            head = head.next;
        }
        System.out.println("]");
    }

    // 两数相加
    static class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode cur1 = l1, cur2 = l2;
            ListNode headNode = new ListNode(0); // 哑节点
            ListNode prev = headNode;            // 尾指针
            int t = 0;                           // 进位

            while (cur1 != null || cur2 != null || t != 0) {
                if (cur1 != null) {
                    t += cur1.val;
                    cur1 = cur1.next;
                }
                if (cur2 != null) {
                    t += cur2.val;
                    cur2 = cur2.next;
                }

                ListNode node = new ListNode(t % 10);
                prev.next = node;
                prev = node;

                t /= 10; // 更新进位
            }
            return headNode.next;
        }
    }

    // 两两交换链表中的节点
    static class Solution2 {
        public ListNode swapPairs(ListNode head) {
            ListNode headNode = new ListNode(0); // 哑节点：固定入口
            headNode.next = head;
            ListNode prev = headNode;
            ListNode cur = head;

            while (cur != null && cur.next != null) {
                ListNode next = cur.next;      // 保存入口：第二个
                ListNode nnext = next.next;    // 保存入口：下一对开头

                // 三步换边
                prev.next = next;               // 左侧接到第二个
                next.next = cur;                // 第二个指回第一个
                cur.next = nnext;               // 第一个接回余下部分

                // 推进到下一对
                prev = cur;                     // prev 指到已交换对的尾
                cur = nnext;                    // cur 跳到下一对的第一个
            }
            return headNode.next;
        }
    }

    // ListNode 定义
    static class ListNode {
        int val;
        ListNode next;

        ListNode() {}

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}